package com.czf.personal.controller;

import com.czf.common.entity.UserBaseInfo;
import com.czf.common.util.AjaxResult;
import com.czf.common.util.AppRedisCacheManager;
import com.czf.personal.service.UserBaseInfoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zfChen
 * @create 2022/4/20 14:51
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/baseInfo")
public class BaseInfoController {

    @Autowired
    private UserBaseInfoServiceImpl userBaseInfoService;

    @Autowired
    private AppRedisCacheManager redisCacheManager;

    @RequestMapping(value = "/hold", method = RequestMethod.POST)
    public AjaxResult insert(@RequestBody UserBaseInfo userBaseInfo) {
        Long userId = userBaseInfo.getUserId();
        UserBaseInfo select = userBaseInfoService.select(userId);
        if (ObjectUtils.isNotEmpty(select)) {
            int update = userBaseInfoService.update(userBaseInfo);
            if (update == 1) {
                redisCacheManager.del(String.valueOf(userId));
                return AjaxResult.success("更新成功");
            }

        } else {
            int insert = userBaseInfoService.insert(userBaseInfo);
            if (insert == 1) {
                redisCacheManager.del(String.valueOf(userId));
                return AjaxResult.success("保存成功");
            }
        }
        return AjaxResult.error();
    }

    @RequestMapping(value = "/select",method = RequestMethod.GET)
    public AjaxResult select(Long userId){
        UserBaseInfo select = userBaseInfoService.select(userId);
        if (ObjectUtils.isNotEmpty(select)){
            return AjaxResult.success(select);
        }
        return AjaxResult.error();
    }
}
